<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>
        返省上报
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/css/main.css}" media="all">
    <script type="text/javascript" th:src="@{/lib/loading/okLoading.js}"></script>
    <!-- 引入ElementUI样式 -->
    <link rel="stylesheet" th:href="@{/css/index.css}">
</head>
<body>
<div id="app">
    <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>返省上报</cite></a>
              <a><cite>{{title}}</cite></a>
            </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
        <el-form :model="province" :rules="rules" ref="provinceFormRef" label-width="120px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="province.name"></el-input>
            </el-form-item>
            <el-form-item label="身份证" prop="idCard">
                <el-input v-model="province.idCard"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input v-model="province.phone"></el-input>
            </el-form-item>
            <el-form-item label="省市区" prop="province">
                <el-cascader :placeholder="initValue" :props="props"
                             @change="changeValue" clearable></el-cascader>
            </el-form-item>
            <el-form-item label="返回时间" prop="returnTime">
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择日期" v-model="province.returnTime"
                                    style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2">——————</el-col>
                <el-col :span="11">
                    <el-time-picker placeholder="选择时间" v-model="province.returnTime"
                                    style="width: 100%;"></el-time-picker>
                </el-col>
            </el-form-item>
            <el-form-item style="display:none">
                <el-input v-model="province.province"></el-input>
            </el-form-item>
            <el-form-item style="display:none">
                <el-input v-model="province.id"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save()" style="text-align: center">保存</el-button>
                <el-button @click="clearAll()" style="text-align: center">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/x-layui.js}" charset="utf-8"></script>
<!--导入Vue依赖包-->
<script th:src="@{/js/vue.js}" charset="utf-8"></script>
<!-- 引入Element组件库 -->
<script th:src="@{/js/index.js}"></script>
<!--导入Axios依赖包-->
<script th:src="@{/js/axios.js}" charset="utf-8"></script>
<script type="text/javascript">
    var app = new Vue({
        el: "#app",
        data: {
            sb: [],
            bool1: false,
            bool2: false,
            initValue: "请选择省市区",
            title: "添加返省",
            province: {id: "", province: ""},
            fileList: [],
            findByCommunityName: [],
            props: {
                lazy: true,
                lazyLoad(node, resolve) {
                    const level = node.level;
                    // console.log(node)
                    const requestDate = {};
                    //省份
                    if (level === 0) {
                        requestDate.type = "province";
                    }
                    if (level === 1) {
                        requestDate.type = "city";
                        requestDate.province_code = node.value;
                    }
                    if (level === 2) {
                        requestDate.type = "area";
                        requestDate.city_code = node.value;
                    }
                    //省市区的接口
                    axios.post("/address/find", requestDate).then((res) => {
                        // console.log(res.data.data)
                        const data = res.data.data;
                        data.forEach(itme => {
                            itme.value = itme.code;
                            itme.label = itme.name;
                            itme.leaf = level >= 2;
                        })
                        //返回节点数据
                        resolve(data);
                    });
                },
            },
            rules: {
                name: [
                    {required: true, message: '请输入返省人员名称', trigger: 'blur'}
                ],
                province: [
                    {required: true, message: '请选择省市区', trigger: 'blur'}
                ],
                idCard: [
                    {required: true, message: '请输入身份证号', trigger: 'blur'}
                ],
                phone: [
                    {required: true, message: '请输手机号', trigger: 'blur'}
                ],
                returnTime:[
                    {required: true, message: '请输入放回时间', trigger: 'blur'}
                ]
            }
        },
        methods: {
            /**默认初始化值**/
            initDefault(value) {
                this.initValue = value.split(",").join(" / ");
            },
            changeValue(value) {
                const requestDate = {};
                this.sb=[];
                requestDate.type = "province";
                requestDate.provinceCode = value[0];
                axios.post("/address/findByCode", requestDate).then((res) => {
                    if (res.data.data != null) {
                        this.sb.push(res.data.data);
                        this.bool1 = true;
                    }
                    if (this.bool1) {
                        requestDate.type = "city";
                        requestDate.cityCode = value[1];
                        axios.post("/address/findByCode", requestDate).then((res) => {
                            if (res.data.data != null) {
                                this.sb.push(res.data.data);
                                this.bool2 = true;
                            }
                            if (this.bool2) {
                                requestDate.type = "area";
                                requestDate.areaCode = value[2];
                                axios.post("/address/findByCode", requestDate).then((res) => {
                                    if (res.data.data != null) {
                                        this.sb.push(res.data.data);
                                        this.province.province = this.sb.join(",");
                                    }
                                })
                            }
                        })
                    }
                })
            },
            findById() {
                axios.get("/province/findById?id=" + this.province.id).then((res) => {
                    this.province = res.data.data;
                    this.initDefault(res.data.data.province);
                });
            },
            //表达重置
            clearAll() {
                this.$refs.provinceFormRef.resetFields();
                if (this.fileList.length > 0) {
                    this.handleRemove();
                }
            },
            save() {
                var method = "";
                if (this.province.id != null) {
                    method = "update"
                } else {
                    method = "add"
                }
                axios.post("/province/" + method, this.province).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                });
            },
            getQueryString(name) {
                let reg = `(^|&)${name}=([^&]*)(&|$)`
                let r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }
        },
        created: function () {
            layui.use(['element', 'layer', 'form'], function () {
                $ = layui.jquery;//jquery
                lement = layui.element();//面包导航
                layer = layui.layer;//弹出层
                form = layui.form();
                okLoading.close($);
            });
            //获取父页面传递参数，小区ID
            this.province.id = this.getQueryString("id");
            //根据小区ID获取小区对象，进行数据回显
            if (this.province.id != null && this.province.id != '') {
                this.title = "修改返省";
                this.findById();
            }
        }
    });
</script>
</body>
</html>
